<template>
  <div class="home-container">
    <!-- 搜索框 -->
    <van-search
      shape="round"
      background="#c82519"
      placeholder="请输入搜索关键词"
    />
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <!-- 轮播图 -->
      <van-swipe :autoplay="3000" indicator-color="white" class="swipe">
        <van-swipe-item
          v-for="item in bannerList"
          :key="item.bannerid"
          class="swipe-item"
        >
          <img :src="item.img" />
        </van-swipe-item>
      </van-swipe>
      <!-- 菜单数据 -->
      <van-grid :column-num="5" :border="false">
        <van-grid-item
          v-for="item in menuList"
          :key="item.id"
          :icon="item.icon"
          :text="item.title"
        />
      </van-grid>
      <!-- 商品列表 -->
      <van-list
        class="goods-list"
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="getGoodsListData"
      >
        <!-- <div
          class="goods-item"
          v-for="item in goodsList"
          :key="item.proid"
          @click="goToDetail(item.proid)"
        > -->
        <router-link
          class="goods-item"
          v-for="item in goodsList"
          :key="item.proid"
          :to="'/detail?proid=' + item.proid"
        >
          <!-- <img :src="item.img1" /> -->
          <img v-img="item.img1" src="@/assets/logo.png" />
          <!-- <img v-lazy="item.img1" /> -->
          <div class="title">{{ item.proname }}</div>
          <div class="desc">{{ item.desc }}</div>
          <div class="price">￥{{ item.originprice | formatPrice }}</div>
        </router-link>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { getBannerList, getMenuList, getGoodsList } from '@/api/home'
export default {
  name: 'Home',
  data () {
    return {
      bannerList: [], // 轮播图数组
      menuList: [], // 菜单列表
      goodsList: [], // 商品列表
      count: 0, // 页码
      loading: false, // 上拉加载是否正在进行
      finished: false, // 是否还有下一页数据
      refreshing: false // 是否正在下拉刷新
    }
  },
  created () {
    this.getBannerListData()
    this.getMenuListData()
    // this.getGoodsListData()
  },
  methods: {
    async getBannerListData () {
      const res = await getBannerList()
      this.bannerList = res.data
    },
    async getMenuListData () {
      const res = await getMenuList()
      this.menuList = res.data
    },
    async getGoodsListData () {
      this.count++
      const res = await getGoodsList({
        count: this.count,
        limitNum: 10
      })
      this.goodsList = [...this.goodsList, ...res.data]

      this.loading = false
      if (res.data.length === 0) {
        // 设置为true,底部会显示提示文字，同时滚动到底部也不会再发送请求了
        this.finished = true
      }

      this.refreshing = false
    },
    onRefresh () {
      this.finished = false
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.goodsList = []
      this.count = 0
      this.getGoodsListData()
    },
    goToDetail (proid) {
      this.$router.push(`/detail/${proid}`)
    }
  },
  filters: {
    formatPrice (value) {
      return value.toFixed(2)
    }
  },
  activated () {
    console.log('---home---activated---')
  },
  deactivated () {
    console.log('---home---deactivated---')
  },
  beforeDestroy () {
    console.log('---home---beforeDestory---')
  },
  destroyed () {
    console.log('---home---destroyed---')
  }
}
</script>

<style lang="scss" scoped>
.home-container {
  padding-bottom: 50px;
  .swipe {
    width: 100%;
    height: 250px;
  }
  .swipe-item {
    width: 100%;
    height: 250px;
  }
  .swipe-item img {
    width: 100%;
    height: 100%;
  }

  .goods-list {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    box-sizing: border-box;
    width: 100%;

    .goods-item {
      box-sizing: border-box;
      background-color: #fff;
      padding: 5.33333vw;
      border-radius: 1.33333vw;
      display: flex;
      align-items: center;
      width: 48%;
      flex-direction: column;
      margin-top: 1.33333vw;
      img {
        width: 26.66667vw;
        height: 32vw;
      }
      .title {
        font-size: 3.73333vw;
        display: inline-block;
        white-space: nowrap;
        width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        margin-top: 4vw;
        color: #000;
      }
      .desc {
        font-size: 3.2vw;
        color: #c3c3c3;
        display: inline-block;
        white-space: nowrap;
        width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        margin-top: 2.66667vw;
      }
      .price {
        font-size: 3.2vw;
        margin-top: 2.66667vw;
        color: red;
      }
    }
  }
}
// 深度作用选择器
::v-deep(.van-list .van-list__loading) {
  width: 100%;
}
::v-deep(.van-list__finished-text) {
  width: 100%;
  text-align: center;
}
</style>
